package com.bicon.botu.account.controller;

import com.bicon.botu.account.dto.RoleDto;
import com.bicon.botu.account.service.IAccountRoleService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 账号角色增删改查接口类
 *
 * @author peizp
 * @date 2017/6/14 0012 下午 2:47
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/accountRole")
public class AccountRoleController extends BaseController {

    @Autowired
    private IAccountRoleService iAccountRoleService;

    /**
     * 增加账号角色关联
     *
     * @param accountId 账号id
     * @param roleId    角色Id,可多个一起传。英文逗号隔开“,”
     * @param type      账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String accountId, @RequestParam String roleId, @RequestParam String type) {
        String[] roleIds = roleId.split(",");
        return iAccountRoleService.saveAccountRoleIds(accountId, roleIds, type);
    }

    /**
     * 删除账号角色关联
     *
     * @param accountId 账号id
     * @param type      账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/delete")
    public BaseResult delete(@RequestParam String accountId, @RequestParam String type) {
        BaseResult baseResult = iAccountRoleService.deleteAccountRoleByAccountId(accountId, type);
        return baseResult;
    }

    /**
     * 查询当前账号下的角色
     *
     * @param accountId 账号Id
     * @param type      账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/selectRolesByAccountId")
    public BaseResult selectRolesByAccountId(@RequestParam String accountId, @RequestParam String type) {
        List<RoleDto> roles = iAccountRoleService.selectRolesByAccountId(accountId, type);
        return sendResult200(roles);
    }

}
